package com.zmn.oms.dubbo.impl.xno;

import com.alibaba.fastjson.JSON;
import com.zmn.common.dto2.ResponseDTO;
import com.zmn.common.utils.reflect.BeanMapper;
import com.zmn.common.utils.string.StringUtil;
import com.zmn.oms.common.constant.RedisKeyConsts;
import com.zmn.oms.common.dio.axb.XnoBindDIO;
import com.zmn.oms.dubbo.interfaces.axb.XnoBindRemoteService;
import com.zmn.oms.dubbo.utils.DubboConsts;
import com.zmn.oms.model.dto.work.xno.XnoBindDTO;
import com.zmn.oms.model.dto.work.xno.XnoUnBindDTO;
import com.zmn.oms.model.entity.detail.OrderDetail;
import com.zmn.oms.services.interfaces.detail.OrderDetailService;
import com.zmn.oms.zmn.business.interfaces.xno.XnoBindBService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.common.constants.LoadbalanceRules;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;
import org.springframework.data.redis.core.RedisTemplate;

import javax.annotation.Resource;
import java.util.Objects;
import java.util.concurrent.TimeUnit;

/**
 * @title: 小号绑定 Dubbo接口实现类
 * @author:Rananquan
 * @date:2022/7/18 18:16
 */
@Slf4j
@DubboService(loadbalance = LoadbalanceRules.ROUND_ROBIN, timeout = 5000, retries = 5,version = DubboConsts.INTERFACE_VERSION, validation = "true")
public class XnoBindRemoteServiceImpl implements XnoBindRemoteService {

    @Resource
    private XnoBindBService xnoBindBService;

    @Resource
    private OrderDetailService orderDetailService;

    @Resource
    private RedisTemplate<String, OrderDetail> orderDetailRedisTemplate;

    @Override
    public ResponseDTO<Boolean> bindXno(XnoBindDIO xnoBindDIO) {
        log.info("小号绑定入参：{}", JSON.toJSONString(xnoBindDIO));

        XnoBindDTO xnoBindDTO = BeanMapper.map(xnoBindDIO, XnoBindDTO.class);
        xnoBindDTO.setUnBindMasterId(xnoBindDIO.getMasterId());
        BeanUtils.copyProperties(xnoBindDIO,xnoBindDTO);

        // 日志记录相关信息
        OrderDetail orderDetail =  this.getOrderDetail(xnoBindDIO.getOrderId());
        setBindTelephone(orderDetail,xnoBindDTO);

        Boolean aBoolean = xnoBindBService.bind(xnoBindDTO);

        return ResponseDTO.success(aBoolean);
    }

    @Override
    public ResponseDTO<Boolean> unBindXno(XnoBindDIO xnoBindDIO) {
        log.info("小号解绑入参：{}",JSON.toJSONString(xnoBindDIO));

        // 日志记录相关信息
        XnoUnBindDTO xnoUnBindDTO = BeanMapper.map(xnoBindDIO, XnoUnBindDTO.class);
        xnoUnBindDTO.setUnBindMasterId(xnoBindDIO.getMasterId());
        BeanUtils.copyProperties(xnoBindDIO,xnoUnBindDTO);

        Boolean aBoolean = xnoBindBService.unBind(xnoUnBindDTO);

        return ResponseDTO.success(aBoolean);
    }


    private void setBindTelephone(OrderDetail orderDetail,XnoBindDTO xnoBindDTO){
        if(StringUtil.isNotBlank(orderDetail.getBindTelephone())){
            xnoBindDTO.setUnBindTelephone1(orderDetail.getBindTelephone());
        }
        if(StringUtil.isNotBlank(orderDetail.getBindTelephone2())){
            xnoBindDTO.setUnBindTelephone1(orderDetail.getBindTelephone2());
        }
        if(StringUtil.isNotBlank(orderDetail.getBindTelephone3())){
            xnoBindDTO.setUnBindTelephone1(orderDetail.getBindTelephone3());
        }
    }

    /**
     * 获取orderDetail
     * @param orderId
     * @return
     */
    private OrderDetail getOrderDetail(Long orderId){
        OrderDetail cacheOrderDetail = orderDetailRedisTemplate.opsForValue().get(String.format(RedisKeyConsts.XNO_BIND_ORDER_DETAIL, orderId));
        if(Objects.isNull(cacheOrderDetail)){
            OrderDetail orderDetail = orderDetailService.findOrderDetailByKey(orderId);
            orderDetailRedisTemplate.opsForValue().set(String.format(RedisKeyConsts.XNO_BIND_ORDER_DETAIL, orderId),orderDetail,10, TimeUnit.MINUTES);

            return orderDetail;
        }

        return cacheOrderDetail;
    }
}
